import os
import base64
import httpx

from fastapi import FastAPI
from langchain.chat_models import AzureChatOpenAI
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langserve import add_routes

os.environ["http_proxy"] = "127.0.0.1:7890"
os.environ["https_proxy"] = "127.0.0.1:7890"
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = "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"

os.environ["OPENAI_API_BASE"] = "http://menshen.test.xdf.cn"
os.environ["OPENAI_API_KEY"] = "c8575027653b42b1b47747f0b4ab135b"
os.environ["OPENAI_API_TYPE"] = "azure"
os.environ["OPENAI_API_VERSION"] = "2023-05-15"

llm = AzureChatOpenAI(
    deployment_name="gpt-4o",
    model_name="gpt-4o",
    temperature=0)

image_url = 'https://pic.rmb.bdstatic.com/bjh/down/6yx1y8Qn3nK4C1CPAoWvjQ1a16ed522eeb235481b68b15310dadf2.jpg?for=bg'
image_url_panda = 'https://se-feed-bucket.bj.bcebos.com/map_aigc_scene_single/955879cf6efe58b7f242c5b09ed0a8b4.jpg'
image_base64 = base64.b64encode(httpx.get(image_url).content).decode('utf-8')
image_base64_panda = base64.b64encode(httpx.get(image_url_panda).content).decode('utf-8')
msg = HumanMessage(
    content=[
        {'type': 'text', 'text': '这两张图片的内容是相同的么'},
        {'type': 'image_url', 'image_url': {'url': f'data:image/jpeg;base64,{image_base64}'}},
        {'type': 'image_url', 'image_url': {'url': f'data:image/jpeg;base64,{image_base64_panda}'}}
    ]
)

resp = llm.invoke([msg])
print(resp)